import 'package:flutter/material.dart';
import 'package:flutter_app/bean/article_entity.dart';
import 'package:flutter_app/utils/DialogUtils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';

import '../../../r.dart';
import 'msg_detail_logic.dart';

/**
 * @Description:  消息详情  消息类型  task_notice_17
 * @Author: 下雨了
 * @CreateDate: 2021-11-16 9:46
 * @UpdateUser: 更新者
 * @UpdateDate:  2022年7月1日 11:07:24
 * @UpdateRemark: getx
 * @Version: 1.1
 */
///
class MsgDetailPage extends StatefulWidget {
  final String title;

  const MsgDetailPage({Key? key, required this.title}) : super(key: key);

  @override
  _MsgDetailPageState createState() => _MsgDetailPageState();
}

class _MsgDetailPageState extends State<MsgDetailPage> {
  final logic = Get.put(MsgDetailLogic());
  final state = Get.find<MsgDetailLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MsgDetailLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: Colors.white,
        appBar: MyAppBar(context, title: "公告通知"),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 35.r),
                child: Center(
                  child: Text(state.articleEntity?.title ?? "",
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Colors.black, fontSize: 17.sp, fontWeight: FontWeight.w600),),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 25.r),
                child: state.articleEntity?.source != "" ?
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text("来源：" + (state.articleEntity?.source ?? ""), style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),),
                      Text("发布时间：" + (state.articleEntity?.publishTime ?? ""), style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),),
                    ],
                  )
                  : Text("发布时间：" + (state.articleEntity?.publishTime ?? ""), style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),),
              ),
              SizedBox(height: 10.r,),
              Divider(indent: 16.r, endIndent: 16.r, color: MyColors.FFDDDDDD, height: 0.r,),
              Container(
                margin: EdgeInsets.only(left: 10.r, right: 10.r),
                child: Html(data: state.articleEntity?.content ?? "",
                  style: {
                    "p": Style(
                        lineHeight: LineHeight(2),
                        fontSize: FontSize(17.sp),
                        fontWeight: FontWeight.w500,
                        direction: TextDirection.ltr,
                        textAlign: TextAlign.justify,
                        letterSpacing: 0.3
                    ),
                    "img": Style(
                      alignment: Alignment.center,
                    ),
                  },
                ),
              ),
              Container(
                alignment: Alignment.centerRight,
                margin: EdgeInsets.only(top: 16.r, right: 16.r, bottom: 20.r),
                child: Text("责任编辑：${state.articleEntity?.updateByName ?? ""}", style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),),
              ),
              Visibility(
                visible: (state.articleEntity?.fileList?.length ?? 0) > 0,
                child: Container(
                  margin: EdgeInsets.all(16.r),
                  child: Material(
                    color: MyColors.FFF5F5F5,
                    child: Container(
                      padding: EdgeInsets.fromLTRB(8.5.r, 8.5.r, 8.5.r, 8.5.r),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text("附件", style: TextStyle(color: MyColors.FF333333, fontSize: 13.sp, fontWeight: FontWeight.w600),),
                          Container(
                            margin: EdgeInsets.only(top: 6.r),
                            child: ListView.builder(
                              shrinkWrap: true, //解决无线高度问题 解决SingleChildScrollView 和listview冲突问题 ListView.builder
                              padding: EdgeInsets.only(top: 0),
                              itemCount: state.articleEntity?.fileList?.length,
                              physics: NeverScrollableScrollPhysics(),
                              itemBuilder: (BuildContext context, int index) {
                                return _DownLoadListItem(state.articleEntity!.fileList![index]);
                              },
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    });
  }

  /// 附件下载列表
  Widget _DownLoadListItem(ArticleFileList item){
    return InkWell(
      child: Container(
        margin: EdgeInsets.only(top: 6.r, bottom: 6.r),
        child: Row(
          children: [
            Container(margin: EdgeInsets.only(top: 2.r, right: 4.5.r),child: Image.asset(R.assetsImgIcon56, width: 14.r,)),
            Text(item.name!, style: TextStyle(color: MyColors.FF2763B9, fontSize: 13.sp),)
          ],
        ),
      ),
      onTap: () {
        dialogUtils.show(
          context: context,
          title: "提示",
          content: "将下载文件到本地",
          callbackOk: () {
            logic.mainLogic.downLoad(item.sourcePath ?? "", item.name ?? "");
          },
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<MsgDetailLogic>();
    super.dispose();
  }
}
